/*
 * @Author: xiaosihan 
 * @Date: 2023-02-06 15:22:57 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-07-02 02:45:35
 */
import { AmbientLight, Box3, DirectionalLight, Object3D, PerspectiveCamera, PointLight, Vector3 } from "three";
import ThreeBase from "three-base";
import threeUtils from "three-base/threeUtils";

import Sky from "three-base/Sky/Sky";
import threeLoader from "three-base/threeLoader";
import Car from "./component3d/Car/Car";
import envMapPNG from "./img/envMap.png";

/**
 *
 *
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {

    constructor() {
        super();
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight("#fff", 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight("#fff", 1.5);
        pointLight.position.set(50, 40, 50);
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight("#fff", 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();

        // 开发模式时还原记录的位置
        if (threeUtils.isDev && localStorage.lookAt) {
            this.resetLookAt({
                ...JSON.parse(localStorage.lookAt)
            });
        }

        // 开发模式时记录当前的位置
        if (threeUtils.isDev) {
            this.controls.addEventListener("change", () => {
                localStorage.lookAt = JSON.stringify(this.getLookAt());
            })
        }

        if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
            this.resetLookAt({
                center: new Vector3(0, 0, 0),
                position: new Vector3(-7.049, 27.469, 59.276)
            });
        }

    }


    //天空
    sky = (() => {
        const sky = new Sky();
        sky.material.map = threeLoader.getTexture(envMapPNG);
        this.scene.add(sky);
        return sky;
    })();


    //汽车模型
    car = (() => {
        const car = new Car();
        car.addEventListener("loaded", (e: any) => {
            this.lookatObjects({
                objects: [car],
                duration: 0,
                direction: new Vector3(1, 0.5, 0),
                offset: new Vector3(0, 0, 0),
                distance: 2.2
            });
        });
        this.scene.add(car);
        return car;
    })();

    // 查看对象
    lookatObjects({
        objects,
        duration = 1000,
        direction = new Vector3(0.3, 0.3, 1),
        offset = new Vector3(0, 0, 0),
        distance = 1
    }: {
        objects: Array<Object3D>,
        duration?: number,
        direction?: Vector3,
        offset?: Vector3,
        distance?: number
    }) {
        const box3 = new Box3();

        objects.map(o => {
            o.traverse(oo => {
                box3.expandByObject(oo);
            });
        });

        const size = new Vector3();
        box3.getSize(size);

        const center = new Vector3();
        box3.getCenter(center);

        center.add(offset);
        const modelSize = size.length() * distance;
        if (this.cameraType === "perspectiveCamera") {
            const { fov } = this.getCamera() as PerspectiveCamera;
            const cameraDistance = modelSize / (2 * Math.tan(fov * Math.PI / 360));
            const position = direction.setLength(cameraDistance).add(center);
            this.setLookAt({ center, position, duration });
        }
    }


}


const homeRenderer = window.homeRenderer = new HomeRender();

export default homeRenderer;